#include "common/uav_texture.h"
#include "common/colours.h"

#include <iostream>

using std::cout;
using std::endl;

static uchar _texture32[32][32][4];
static uchar _texture16[16][16][4];
static uchar _texture8[8][8][4];
static uchar _texture4[4][4][4];
static uchar _texture2[2][2][4];
static uchar _texture1[1][1][4];

/**
  * The Uav texture constructor
  * colour - the colour of the texture
  */
Uav_texture::Uav_texture(const QString &colour) :  _colour(3)
{
    _is_created = false;
    get_color(colour, _colour);
}

Uav_texture::~Uav_texture()
{
}

/**
  * Makes the texture if it's not created.
  */
void Uav_texture::make_texture()
{
    if(! _is_created)
    {

        QVector<int> black(3);
        get_color("BLACK", black);
        for (int i = 0; i < 32; i++)
          for (int j = 0; j < 32; j++)
          {
            QVector<int> &c = (i == 0 || i == 31 || j == 31 || j == 0)? black : _colour;
            _texture32[i][j][0] = (uchar) c.at(0);
            _texture32[i][j][1] = (uchar) c.at(1);
            _texture32[i][j][2] = (uchar) c.at(2);
            _texture32[i][j][3] = (uchar) 255;
          }

        for (int i = 0; i < 16; i++)
          for (int j = 0; j < 16; j++)
          {
            QVector<int> &c = (i == 0 || i == 15 || j == 15 || j == 0)? black : _colour;
            _texture16[i][j][0] = (uchar) c.at(0);
            _texture16[i][j][1] = (uchar) c.at(1);
            _texture16[i][j][2] = (uchar) c.at(2);
            _texture16[i][j][3] = (uchar) 255;
          }

        for (int i = 0; i < 8; i++)
          for (int j = 0; j < 8; j++)
          {
            QVector<int> &c = (i == 0 || i == 7 || j == 7 || j == 0)? black : _colour;
            _texture8[i][j][0] = (uchar) c.at(0);
            _texture8[i][j][1] = (uchar) c.at(1);
            _texture8[i][j][2] = (uchar) c.at(2);
            _texture8[i][j][3] = (uchar) 255;
          }

        for (int i = 0; i < 4; i++)
          for (int j = 0; j < 4; j++)
          {
            QVector<int> &c = (i == 0 || i == 3 || j == 3 || j == 0)? black : _colour;
            _texture4[i][j][0] = (uchar) c.at(0);
            _texture4[i][j][1] = (uchar) c.at(1);
            _texture4[i][j][2] = (uchar) c.at(2);
            _texture4[i][j][3] = (uchar) 255;
          }

        for (int i = 0; i < 2; i++)
          for (int j = 0; j < 2; j++)
          {
            QVector<int> &c = (i == 0 || i == 1 || j == 1 || j == 0)? black : _colour;
            _texture2[i][j][0] = (uchar) c.at(0);
            _texture2[i][j][1] = (uchar) c.at(1);
            _texture2[i][j][2] = (uchar) c.at(2);
            _texture2[i][j][3] = (uchar) 255;
          }

        for (int i = 0; i < 1; i++)
          for (int j = 0; j < 1; j++)
          {
            QVector<int> &c = _colour;
            _texture1[i][j][0] = (uchar) c.at(0);
            _texture1[i][j][1] = (uchar) c.at(1);
            _texture1[i][j][2] = (uchar) c.at(2);
            _texture1[i][j][3] = (uchar) 255;
          }

        _is_created = true;
    }
}

/**
  * Returns a texture
  * size - the size of the texture in pixels. (size * size)
  */
uchar *Uav_texture::get_texture(int size)
{
    if(not _is_created)
        make_texture();

    if(size == 32)
        return (uchar *)  _texture32;
    else if(size == 16)
        return (uchar *)  _texture16;
    else if(size == 8)
        return (uchar *)  _texture8;
    else if(size == 4)
        return (uchar *)  _texture4;
    else if(size == 2)
        return (uchar *)  _texture2;
    else if(size == 1)
        return (uchar *)  _texture1;
}

//
// PRIVATE METHODS
//

void Uav_texture::make_mipmap(int _width, int _height, void *_texture)
{
    cout << "MAKE MIPMAP : " << _width << endl;
//    for (int i = 0; i < _height; i++)
//      for (int j = 0; j < _width; j++)
//      {
//        int c = (i == 0 || i == (_height - 1) || j == (_width - 1) || j == 0)? 0 : 152;
//        cout << "fst" << endl;
//        (static_cast<uchar [_height][_width][4]> (_texture))[i][j][0] = (uchar) c;
//        cout << "snd" << endl;
//        ((uchar ***) _texture)[i][j][1] = (uchar) c;
//        cout << "trd" << endl;
//        ((uchar ***) _texture)[i][j][2] = (uchar) c;
//        cout << "forth" << endl;
//        ((uchar ***) _texture)[i][j][3] = (uchar) 255;
//      }
    cout << "MIPMAP : " << _width << " Made!!" << endl;
}
